package com.jcl.recruit.apply.service;

import com.jcl.recruit.apply.mapper.InterviewMapper;
import com.jcl.recruit.apply.mapper.InterviewSkdMapper;
import com.jcl.recruit.apply.model.InterviewSkd;
import com.jcl.recruit.resume.logic.ResumeLogic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 刘明新
 * @date 2018/8/6 下午3:06
 */
@Service
public class InterviewSkdService implements InterviewSkdMapper {

    @Autowired
    private ResumeLogic resumeLogic;

    @Autowired
    private InterviewSkdMapper interviewSkdMapper;

    @Autowired
    private InterviewMapper interviewMapper;

    @Override
    public int insertSelective(InterviewSkd record) {
        return interviewSkdMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(InterviewSkd record) {
        return interviewSkdMapper.updateByPrimaryKeySelective(record);
    }

    public void insertOrUpdate(InterviewSkd interviewSkd){
        if(interviewSkd.getId() == null){
            this.insertSelective(interviewSkd);
        }else{
            this.updateByPrimaryKeySelective(interviewSkd);
        }
    }

    @Override
    public List<Map<String,Object>> selectInterviewSkdsByDate(Date scheduleStartDate, Date scheduleEndDate,String token,Integer interviewId) {
        List<Map<String,Object>> lists = interviewSkdMapper.selectInterviewSkdsByDate(scheduleStartDate,scheduleEndDate,token,interviewId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(Map<String,Object> entity:lists){
            if(entity.get("createtime") != null) {
                entity.put("createtime", sdf.format(entity.get("createtime")));
            }
        }
        return lists;
    }

    @Override
    public Map<String,Object> selectPrimaryKeyById(Integer id) {
        Map<String,Object> map = interviewSkdMapper.selectPrimaryKeyById(id);
        if(map == null || map.get("interview_id") == null){
            return map;
        }
        Map<String,Object> interviewMap = interviewMapper.selectPrimaryKeyById((Integer)map.get("interview_id"));
        map.put("interview",interviewMap);
        map.put("resume",resumeLogic.selectResumeByPrimsryKeyIdMap((Integer) interviewMap.get("resume_id")));
        return map;
    }

    @Override
    public List<InterviewSkd> selectAllInterviewSkdsByInterviewId(Integer interviewId) {
        return interviewSkdMapper.selectAllInterviewSkdsByInterviewId(interviewId);
    }

    @Override
    public int selectCountByScheduleOptions(Date date, Integer interviewId, Integer hrId) {
        return interviewSkdMapper.selectCountByScheduleOptions(date,interviewId,hrId);
    }
}
